package lojaVirtual.manterCadastro.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;



import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

//import lojaVirtual.pojo.Usuario;

@ManagedBean
@SessionScoped
public class UploadBean {

	/*
	 * @ManagedProperty(value="#{loginBean}") private LoginBean loginBean;
	 * private Usuario usuario;
	 */


	public UploadBean() {

		// this.usuario = loginBean.getUsuarioAutenticado();
	}

	public void handleFileUpload(FileUploadEvent event) {
		

		
		
		UploadedFile arq = event.getFile();
		try {
			InputStream in = new BufferedInputStream(arq.getInputstream());
			File file = new File("C://tmp//" + arq.getFileName());
			
			//String caminho = file.getAbsolutePath();
			FileOutputStream out = new FileOutputStream(file);
			
			while(in.available() != 0){
				
				out.write(in.read());
			}
			
			out.close();
			
			
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}

		FacesMessage msg = new FacesMessage("Enviado!", event.getFile()
				.getFileName() + " enviado(a) com sucesso.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

}
